# -*- coding: utf-8 -*-

from functools import partial
from flask import Blueprint, url_for

lenta_blueprint = Blueprint('lenta', __name__)


def register(app, url_prefix, options, prototype=False):
    """ 
        :app
        :url_prefix
        :options
            ::db_backend
            ::login_required
        :prototype
    """
    if prototype:
        from .views.prototype import lenta as lenta_views, note as note_views, tag as tag_views, \
                root_page, browse_lenta, get_notes_by_tag
    else:
        from .views import lenta as lenta_views, note as note_views, tag as tag_views, \
                root_page, browse_lenta, get_notes_by_tag
    
    
    login_required = options['login_required']    
    
    # root page
    
    lenta_blueprint.add_url_rule('/',
        'root',
        root_page,
        methods=['GET']
    )
    
    lenta_blueprint.add_url_rule('/<namespace>/<int:lenta_id>',
        'view_lenta', 
        browse_lenta,
        methods=['GET']
    )
    
    lenta_blueprint.add_url_rule('/<namespace>/<int:lenta_id>/note/tag/<tag_name>',
                          'get_notes_by_tag', 
                          get_notes_by_tag,
                          methods=['GET']
    )
    
    #*******#
    # lenta #
    #*******#
    
    lenta_blueprint.add_url_rule('/add',
        'add_lenta', 
        login_required(lenta_views.add_lenta),
        methods=['GET', 'POST']
    )
    lenta_blueprint.add_url_rule('/<namespace>/<int:lenta_id>/info',
        'lenta_info', 
        lenta_views.info,
        methods=['GET']
    )
    lenta_blueprint.add_url_rule('/<int:lenta_id>/edit',
        'edit_lenta', 
        login_required(lenta_views.edit_lenta),
        methods=['GET', 'POST']
    )
    lenta_blueprint.add_url_rule('/<int:lenta_id>/del',
        'delete_lenta', 
        login_required(lenta_views.delete_lenta),
        methods=['GET', 'POST']
    )
        
    #******#
    # note #
    #******#
       
    lenta_blueprint.add_url_rule('/<int:lenta_id>/note/add', 'add_note', 
                          login_required(note_views.add_note),
                          methods=['GET', 'POST']
    )
    lenta_blueprint.add_url_rule('/<namespace>/<int:lenta_id>/note/<int:note_id>',
                          'view_note', 
                          note_views.view_note,
                          methods=['GET']
    )
    lenta_blueprint.add_url_rule('/<int:lenta_id>/note/<int:note_id>/edit',
                          'edit_note', 
                          login_required(note_views.edit_note),
                          methods=['GET', 'POST']
    )
    lenta_blueprint.add_url_rule('/<int:lenta_id>/note/<int:note_id>/del', 'delete_note', 
                          login_required(note_views.delete_note),
                          methods=['GET', 'POST']
    )
        
    #******#
    # tags #
    #******#
        
    lenta_blueprint.add_url_rule('/<int:lenta_id>/tag/add', 'add_tag', 
                          login_required(tag_views.add_tag),
                          methods=['GET', 'POST']
    )
    lenta_blueprint.add_url_rule('/<namespace>/<int:lenta_id>/tag/<tag_name>', 'view_tag', 
                          tag_views.view_tag,
                          methods=['GET']
    )
    lenta_blueprint.add_url_rule('/<int:lenta_id>/tag/<tag_name>/edit', 'edit_tag', 
                          login_required(tag_views.edit_tag),
                          methods=['GET', 'POST']
    )
    lenta_blueprint.add_url_rule('/<int:lenta_id>/tag/<tag_name>/del', 'delete_tag', 
                          login_required(tag_views.delete_tag),
                          methods=['GET', 'POST']
    )
    lenta_blueprint.add_url_rule('/<namespace>/<int:lenta_id>/tag/all', 'view_all_tags_for_lenta', 
                          tag_views.view_all_tags_for_lenta,
                          methods=['GET']
    )
    
    
    app.register_blueprint(lenta_blueprint, url_prefix=url_prefix)
    